//
//  UserManager.m
//  UserInfoManager
//
//  Created by imac on 2017/6/22.
//  Copyright © 2017年 LiuShun. All rights reserved.
//

#import "UserManager.h"
#import "LoginViewController.h"

static UserManager *_sharedManager;

static NSString *const KEY_USERDATA = @"userData";

#define UserDefaults(key) [[NSUserDefaults standardUserDefaults]objectForKey:key]

@implementation UserManager


- (void)saveUserData:(id)data{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    [userDefault setObject:data forKey:KEY_USERDATA];
    
    [userDefault synchronize];
    
}
- (void)delateAllUserData{
    
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    
    [userDefault removeObjectForKey:KEY_USERDATA];
    
    [userDefault synchronize];
    
}
- (UserInfo *)user{
    
    return [UserInfo mj_objectWithKeyValues:UserDefaults(KEY_USERDATA)];
    
}

- (NSString *)uid{
    
    return [[UserManager defaultManager] user].uid;
    
}

- (BOOL)isLogin{
    
    if ([[UserManager defaultManager] uid].length == 0 ) {
        
        return NO;
        
    }else{
        
        return YES;
        
    }
    
}

- (void)goToLoginViewController:(UIViewController *)controller{
    
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    
    [controller presentViewController:loginVC animated:YES completion:nil];
    
}

+ (instancetype)defaultManager{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [[self alloc] init];
    });
    return _sharedManager;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManager = [super allocWithZone:zone];
    });
    return _sharedManager;
}
// 为了严谨，也要重写copyWithZone 和 mutableCopyWithZone
-(id)copyWithZone:(NSZone *)zone
{
    return _sharedManager;
}
-(id)mutableCopyWithZone:(NSZone *)zone
{
    return _sharedManager;
}
@end
